package com.isorf.controller;

import com.github.pagehelper.PageInfo;
import com.isorf.pojo.brand;
import com.isorf.pojo.color;
import com.isorf.pojo.size;
import com.isorf.service.Linkservice;
import com.isorf.service.UploadService;
import com.isorf.service.shoeservice;
import com.isorf.service.sizeservice;
import com.isorf.util.responseresult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
public class sizecontroller {
    @Autowired
    private sizeservice sizeservice;
    @Autowired
    private Linkservice linkservice;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private com.isorf.service.shoeservice shoeservice;
    @Value("${file.upload.path}")     // 从application.yml文件中获取图片上传的路径
    private String path;

    @GetMapping("/getallsize")
    public responseresult<List<size>> getallsize() {
        List<size> sizeList = sizeservice.getallsize();
        if (sizeList == null) {
            return responseresult.failure(508, "没有学生信息");
        }else {
            return responseresult.success(200, "查询成功", sizeList);

        }
    }

    @PostMapping("/deletesize")
    public responseresult<Boolean> deletesize(@RequestBody size size) {
        // 检查link对象和id是否为空
        if (size == null || size.getSizeId() == null) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的删除方法，应该是一个删除方法而不是addlink
        Boolean result = sizeservice.deletesize(size.getSizeId());
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "删除失败");
        }
    }

    @PostMapping("/batchDdeeeelete")
    public responseresult<Boolean> batchDelete(@RequestBody List<Integer> ids) {
        // 检查参数
        if (ids == null || ids.isEmpty()) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的批量删除方法
        Boolean result = sizeservice.batchDelete(ids);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "批量删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "批量删除失败");
        }
    }

    @PostMapping("/paaggggeList")
    public responseresult<PageInfo<size>> pageList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo<size> result = sizeservice.pageList(pageNum, pageSize);
        if (result==null&&result.getList().size()==0) {
            return responseresult.failure(responseresult.FAILED, "查询用户列表失败");
        }else {
            return responseresult.success(responseresult.SUCCESS,"查询成功", result);
        }
    }


    @PostMapping("/addsize")
    public responseresult<Boolean> addsize(@RequestBody size size) {
        Boolean result = sizeservice.addsize(size);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "Link successful", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "Link failed");
        }
        // Remove the unreachable statement
    }

    @PostMapping("/updatesize")
    public responseresult<Boolean> updatesize(@RequestBody size size) {
        if (size == null || size.getSizeId() == null) {
            return responseresult.failure(responseresult.FAILED, "更新失败，缺少必要参数");
        }
        Boolean result = sizeservice.updatecolor(size);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "更新成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "更新失败");
        }
    }






}
